/*   Copyright (C) 2008  Jeroen v. Schagen & Youri Lima
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;

using Engine.Math;

namespace Engine.Rendering
{
    /// <summary>
    ///     Yourie's ray of destruction. Pimped by Jeroen!
    /// </summary>
    public class Ray
    {
        private Vector3 origin;
        private Vector3 direction;

        #region Constructor

        /// <summary>
        ///     Default no-arg constructor.
        /// </summary>
        public Ray()
        {
            this.origin = Vector3.ZERO;
            this.direction = Vector3.UNIT_Z;
        }

        /// <summary>
        ///     Construct ray using origin and direction vector.
        /// </summary>
        /// 
        /// <param name="origin"></param>
        /// <param name="direction"></param>
        public Ray(Vector3 origin, Vector3 direction)
        {
            this.origin = origin;
            this.direction = direction;
        }

        #endregion


        #region Function

        /// <summary>
        ///     Retrieves a point.
        /// </summary>
        /// 
        /// <param name="t"></param>
        /// <returns></returns>
        public Vector3 Point(float t)
        {
            return ( origin + ( direction * t ) );
        }

        /// <summary>
        ///     Retrieve intersection.
        /// </summary>
        /// 
        /// <param name="p"></param>
        /// <returns></returns>
        public float Intersects(Plane p)
        {
            Vector3 normal = new Vector3(p.normal.x, p.normal.y, p.normal.z);
            float d = normal.DotProduct(direction);

            if (System.Math.Abs(d) < float.Epsilon)
            {
                // Parallel
                return 0;
            }

            else
            {
                float norm = normal.DotProduct(origin) + p.d;
                float t = -(norm / d);

                return t;
            }
        }

        #endregion


        # region Accessor

        /// <summary>
        ///     Origin
        /// </summary>
        public Vector3 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        /// <summary>
        ///     Direction
        /// </summary>
        public Vector3 Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        # endregion


        #region Converter

        /// <summary>
        ///     Convert this ray into a string.
        /// </summary>
        /// 
        /// <returns></returns>
        public override String ToString()
        {
            return origin.ToString() + "; " + direction.ToString();
        }

        #endregion
    }
}
